package com.zhuiyi.yiqingbackend.service.impl;

import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.domain.Base;
import com.zhuiyi.yiqingbackend.domain.Master;
import com.zhuiyi.yiqingbackend.domain.request.MasterAddRequest;
import com.zhuiyi.yiqingbackend.domain.request.MasterUpdateRequest;
import com.zhuiyi.yiqingbackend.domain.request.MasterUpdateStatuRequest;
import com.zhuiyi.yiqingbackend.domain.response.MasterResponse;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.mapper.BaseMapper;
import com.zhuiyi.yiqingbackend.mapper.MasterMapper;
import com.zhuiyi.yiqingbackend.service.BaseService;
import com.zhuiyi.yiqingbackend.service.MasterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class MasterServiceImpl implements MasterService {

    @Resource
    private MasterMapper masterMapper;
    @Resource
    private BaseService baseService;
    @Resource
    private BaseMapper baseMapper;

    @Override
    public List<MasterResponse> getMasters() {
        List<MasterResponse> list = new ArrayList<>();
        List<Master> masters = masterMapper.getMasters();
        for(int i=0;i<masters.size();i++){
            Master master = masters.get(i);
            int baseId = master.getBaseId();
            Base base = baseMapper.getBaseById(baseId);
            MasterResponse masterResponse = new MasterResponse(base,master);
            list.add(masterResponse);
        }
        return list;
    }

    @Override
    public boolean addMaster(MasterAddRequest request) {
        String phone = request.getPhone();
        Base base = baseService.getBaseByPhone(phone);
        if(base==null){
            Master master = new Master(request);
            return masterMapper.addMaster(master);
        }else {
            int baseId = base.getId();
            Master master = new Master(request);
            master.setBaseId(baseId);
            return masterMapper.addMaster(master);
        }
    }

    @Override
    public boolean deleteMasterById(int id) {
        Master master = masterMapper.getMasterById(id);
        if(master == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "删除失败");
        }
        return masterMapper.deleteMasterById(id);
    }

    @Override
    public MasterResponse getMasterById(int id) {
        if(id <= 0 || StringUtils.isBlank(Integer.toString(id))){
            throw new BusinessException(ErrorCode.PARAM_ERROR, "id错误");
        }
        Master master = masterMapper.getMasterById(id);
        if(master == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该事件不存在");
        }
        int baseId = master.getBaseId();
        Base base = baseMapper.getBaseById(baseId);
        MasterResponse masterResponse = new MasterResponse(base,master);
        return masterResponse;
    }

    @Override
    public boolean updateMaster(MasterUpdateRequest request) {
        int id = request.getId();
        String time = request.getTime();
        String title = request.getTitle();
        String message = request.getMessage();
        Master master = new Master(id,time,title,message);
        Master check = masterMapper.getMasterById(id);
        if(check == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该事件不存在");
        }
        return  masterMapper.updateMaster(master);

    }

    @Override
    public boolean updateMasterStatus(MasterUpdateStatuRequest request) {
        int id = request.getId();
        int estatus = request.getEstatus();
        Master check = masterMapper.getMasterById(id);
        if(check == null){
            throw new BusinessException(ErrorCode.DATA_NOT_EXIST, "该事件不存在");
        }
        check.setEstatus(estatus);

        return masterMapper.updateMasterStatus(check);

    }


}
